/**
  ******************************************************************************
  * @file    hda_diskio.c
  * @author  legend_1981@163.com
  * @version V1.0.0
  * @date    2019.5.21
  * @brief   HDA Disk I/O driver
  ******************************************************************************
  
  */ 

/* Includes ------------------------------------------------------------------*/
#include <string.h>
#include "ff_gen_drv.h"
#include "hda_diskio.h"
#include "apilib.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Block Size in Bytes */
#define BLOCK_SIZE                512

/* Private variables ---------------------------------------------------------*/
/* Disk status */
static volatile DSTATUS Stat = STA_NOINIT;

/* Private function prototypes -----------------------------------------------*/
DSTATUS HDA_initialize (void);
DSTATUS HDA_status (void);
DRESULT HDA_read (BYTE*, DWORD, BYTE);
DRESULT HDA_write (const BYTE*, DWORD, BYTE);
DRESULT HDA_ioctl (BYTE, void*);

  
Diskio_drvTypeDef  HDA_Driver =
{
  HDA_initialize,
  HDA_status,
  HDA_read, 
  HDA_write,
  HDA_ioctl,

};

/* Private functions ---------------------------------------------------------*/

/**
  * @brief  Initializes a Drive
  * @param  None
  * @retval DSTATUS: Operation status
  */
DSTATUS HDA_initialize(void)
{
  Stat = STA_NOINIT;
  
  /* Configure the uHDA device */
  Stat &= ~STA_NOINIT;

  return Stat;
}

/**
  * @brief  Gets Disk Status
  * @param  None
  * @retval DSTATUS: Operation status
  */
DSTATUS HDA_status(void)
{
  Stat = STA_NOINIT;

  Stat &= ~STA_NOINIT;
  
  return Stat;
}

/**
  * @brief  Reads Sector(s)
  * @param  *buff: Data buffer to store read data
  * @param  sector: Sector address (LBA)
  * @param  count: Number of sectors to read (1..128)
  * @retval DRESULT: Operation result
  */
DRESULT HDA_read(BYTE *buff, DWORD sector, BYTE count)
{
  DRESULT res = RES_OK;
  int i;
  //api_putstr0("read hard disk");
  for(i=0; i<count; i++)
  {
    api_read_hard_disk_0(sector+i, &buff[512*i]);
    //printf(" %d ", sector+i);
    
  }
  //api_putstr0("\r\n");
  return res;
}

/**
  * @brief  Writes Sector(s)
  * @param  *buff: Data to be written
  * @param  sector: Sector address (LBA)
  * @param  count: Number of sectors to write (1..128)
  * @retval DRESULT: Operation result
  */

DRESULT HDA_write(const BYTE *buff, DWORD sector, BYTE count)
{
  DRESULT res;

  int i;
  //api_putstr0("write hard disk");
  for(i=0; i<count; i++)
  {
    api_write_hard_disk_0(sector+i, &buff[512*i]);
    //printf(" %d ", sector+i);
    
  }
  //api_putstr0("\r\n");
  res = RES_OK;
  return res;
}


/**
  * @brief  I/O control operation
  * @param  cmd: Control code
  * @param  *buff: Buffer to send/receive control data
  * @retval DRESULT: Operation result
  */


DWORD HDA_GetSectorCount(void)
{
    return 8*1024*1024/BLOCK_SIZE;  // 8MB
}
DRESULT HDA_ioctl(BYTE cmd, void *buff)
{
  DRESULT res = RES_ERROR;
  //HAL_HDA_CardInfoTypedef CardInfo;
  
  if (Stat & STA_NOINIT) return RES_NOTRDY;
  
  switch (cmd)
  {
  /* Make sure that no pending write process */
  case CTRL_SYNC :
    res = RES_OK;
    break;
  
  /* Get number of sectors on the disk (DWORD) */
  case GET_SECTOR_COUNT :
    *(DWORD*)buff = HDA_GetSectorCount();
    res = RES_OK;
    break;
  
  /* Get R/W sector size (WORD) */
  case GET_SECTOR_SIZE :
    *(WORD*)buff = BLOCK_SIZE;
    res = RES_OK;
    break;
  
  /* Get erase block size in unit of sector (DWORD) */
  case GET_BLOCK_SIZE :
    *(DWORD*)buff = BLOCK_SIZE;
    break;
  
  default:
    res = RES_PARERR;
  }
  
  return res;
}

  
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

